package com.junshang.JUC_day1;

import static java.lang.Thread.sleep;

/**
 * Created by sahngjun on 2017/3/17.
 */
public class TestVolatile {
    public static void main(String[] args) {
        ThreadDemo td = new ThreadDemo();
        new Thread(td).start();

        while (true) {
            if (td.flag) {
                System.out.println("-----------------------------");
                break;
            }
        }
    }
}

class ThreadDemo implements Runnable {
    public volatile boolean flag = false;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        flag = true;
        System.out.println("flag=" + isFlag());
    }
}
